package com.enniswu.weitter.setting.theme;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseActivity;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.Theme;
import com.enniswu.weitter.widget.adapter.OnItemClickListener;
import com.enniswu.weitter.widget.adapter.ThemeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import skin.support.SkinCompatManager;

public class ThemeActivity extends BaseActivity implements OnItemClickListener {

    @BindView(R.id.rv_theme)
    RecyclerView rvTheme;
    private String current;
    private List<Theme> themes;
    private ThemeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme);
        setTitle(R.string.center_theme);
        current = SPSupport.getInstance().getTheme();
        themes = new ArrayList<>();
        themes.add(new Theme("默认", "de", R.color.colorAccent));
        themes.add(new Theme("天空蓝", "sky.skin", R.color.colorAccent));
        themes.add(new Theme("原谅色", "green.skin", R.color.colorAccent));
        adapter = new ThemeAdapter(themes);
        adapter.setCurrent(current);
        adapter.setClickListener(this);
        rvTheme.setLayoutManager(new GridLayoutManager(this, 3));
        rvTheme.setAdapter(adapter);
    }

    @Override
    public void onItemClick(View v, int position) {
        Theme theme = themes.get(position);
        if (current.equals(theme.getSkinName()))
            return;
        if ("de".equals(theme.getSkinName())) {
            SkinCompatManager.getInstance().restoreDefaultTheme();
        } else {
            SkinCompatManager.getInstance().loadSkin(theme.getSkinName());
        }
        current = theme.getSkinName();
        SPSupport.getInstance().putTheme(current);
        adapter.setCurrent(current);
        adapter.notifyDataSetChanged();
    }
}
